package demo;

import demo.easyExcel.DemoData;
import demo.easyExcel.DemoData2;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: zjq
 * @Date: create in 2022/7/13 15:21
 */
//list相关测试
public class testList {


    //分批次获取list中的数据
    @Test
    public void batchGet() {
        // 初始化list数据
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 11; i++) {
            list.add("" + i);
        }
        System.out.println("原始list：" + list);
        int SIZE_5 = 5;
        // 分批获取(每5个元素获取一个子集合)
        for (int i = 0; i < list.size() / SIZE_5 + 1; i++) {
            int fromIndex = i * SIZE_5;
            int toIndex = Math.min(i * SIZE_5 + SIZE_5, list.size());
            List<String> subList = list.subList(fromIndex, toIndex); // 注意:这里可能出现List为空的情况，所以下面需要有对List集合的判断
            if (CollectionUtils.isNotEmpty(subList)) {
                System.out.println(subList);
                // ... 处理subList集合
            }
        }
    }

    //list 交并集
    @Test
    public void isContail() {
        List<String> a = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            a.add(i + "");
        }
        List<String> b = new ArrayList<String>();
        for (int i = 0; i < 4; i++) {
            b.add(i + "");
        }
        b.add("11");

        System.out.println("a=" + a);
        System.out.println("b=" + b);

        //1.交集
        Collection<String> intersection = CollectionUtils.intersection(a, b);
        System.out.println("交集" + intersection);
        a.retainAll(b);
        System.out.println("交集" + a);

        // 2.并集
        //Collection<String> union = CollectionUtils.union(a, b);
        //System.out.println("并集" + union);
        //a.removeAll(b);
        //a.addAll(b);
        //System.out.println("并集" + a);

        //3.差集
        //Collection<String> subtract = CollectionUtils.subtract(a, b);
        //System.out.println("差集a-b" + subtract);
        //Collection<String> subtract1 = CollectionUtils.subtract(b, a);
        //System.out.println("差集b-a" + subtract1);
        //a.removeAll(b);
        //System.out.println("差集" + a);
    }

    //List对象中某字段值存在于另一个List集合中并过滤数据
    @Test
    public void contailToList() {
        List<DemoData> lista = new ArrayList();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            lista.add(data);
        }

        List<String> listb = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            listb.add("字符串" + i);
        }
        listb.add("字符串" + 11);

        List<String> idList = lista.stream().map(DemoData::getString).collect(Collectors.toList());

        System.out.println(lista);
        System.out.println("原来的" + idList);
        System.out.println("处理的" + listb);

        idList.retainAll(listb);
        System.out.println("交集" + idList);

        List<DemoData> collect = lista.stream()
                .filter(u -> idList.contains(u.getString()))
                .collect(Collectors.toList());
        System.out.println(collect);
        List<String> idLists = collect.stream().map(DemoData::getString).collect(Collectors.toList());
        System.out.println("找到的" + idLists);

        lista.removeAll(collect);//删除找到的
        System.out.println("剩余的" + lista);
    }

    //找两个list中相同下标的值
    @Test
    public void samesize() {
        List<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        a.add(3);
        a.add(5);
        List<String> b = new ArrayList<>();
        b.add("a");
        b.add("b");
        b.add("c");
        b.add("d");

        List<String> c = new ArrayList<>();
        c.add("a");
        c.add("c");

        List<Integer> num = new ArrayList<>();
        List<String> collect = b.stream()
                .filter(u -> c.contains(u))
                .collect(Collectors.toList());
        for (int i = 0; i < b.size(); i++) {
            for (int j = 0; j < c.size(); j++) {
                if (collect.get(j).equals(b.get(i))) {
                    num.add(i + 1);
                }
            }
        }
        b.removeAll(c);
        System.out.println("得到的" + collect);
        System.out.println("得到的下标" + num);
        System.out.println("剩余的" + b);
        List<Integer> collect1 = a.stream()
                .filter(u -> num.contains(a.indexOf(u)))
                .collect(Collectors.toList());
        System.out.println("剩余的1111" + collect1);


    }

    //获取list对象中所有重复的List
    @Test
    public void getAllRepeat() {
        List<DemoData> readlist = new ArrayList();//原始数据
        for (int i = 0; i < 2; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            readlist.add(data);
        }
        //重复数据
        DemoData data = new DemoData();
        data.setString("字符串" + 1);
        data.setDate(new Date());
        data.setDoubleData(0.56);
        readlist.add(data);
        readlist.add(data);

        System.out.println("原始数据" + readlist);
        System.out.println("原始数据的长度" + readlist.size());

        List<DemoData> listError = new ArrayList();//重复的数据
        List<DemoData> listtodel = new ArrayList();//取出重复数据之后的数据

        //取出重复的，id有两个以上一样的
        Map<String, Long> muCount = readlist.stream().collect(Collectors.groupingBy(DemoData::getString, Collectors.counting()));
        List<String> muIds = muCount.entrySet().stream().filter(e -> e.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());

        if (CollectionUtils.isNotEmpty(muIds)) {
            System.out.println("重复的string值=" + muIds + "重复的string值的大小=" + muIds.size());
            for (String muId : muIds) {
                for (int i = 0; i < readlist.size(); i++) {
                    if (readlist.get(i).getString().equals(muId)) {
                        listError.add(readlist.get(i));
                    } else {
                        listtodel.add(readlist.get(i));
                    }
                }
            }
        } else {
            listtodel = readlist;
        }
        System.out.println("重复数据数据" + listError);
        System.out.println("处理后的数据数据" + listtodel);

    }

    // List 转 Array数组
    @Test
    public void listToArray() {
        List<String> list1 = new ArrayList<>();
        list1.add("1");
        list1.add("阿萨");
        list1.add("b");
        System.out.println("list1是" + list1);

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(2);

        //1.方法一
        // String
        String[] strs = list1.toArray(new String[list1.size()]);
        System.out.println("法1String=" + Arrays.toString(strs));
        //Integer
        Integer[] a = list.toArray(new Integer[list.size()]);
        System.out.println("法1Integer=" + Arrays.toString(a));

        //2.方法二
        int[] b = list.stream().mapToInt(Integer::valueOf).toArray();
        System.out.println("法2Integer=" + Arrays.toString(b));
    }

    //过滤list对象中一个字段得到新的list
    @Test
    public void getNewList() {
        List<DemoData> readlist = new ArrayList();//原始数据
        for (int i = 0; i < 2; i++) {
            DemoData data = new DemoData();
            data.setString("字符串" + i);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            readlist.add(data);
        }
        System.out.println("原来的" + readlist);
        List<DemoData2> b = readlist.stream().map(e -> {
            DemoData2 a = new DemoData2();
            BeanUtils.copyProperties(e, a);
            return a;
        }).collect(Collectors.toList());
        System.out.println("新的" + b);
    }

    //list相加合并
    @Test
    public void addAll() {
        List<String> headerList = new ArrayList<String>();//评分类型：1打等第2打星3文字描述
        List<String> headerList1 = new ArrayList<String>();//临时的---1打等第
        List<String> headerList2 = new ArrayList<String>();//临时的---2打星
        List<String> headerList3 = new ArrayList<String>();//临时的---3文字描述
        headerList.add("姓名");
        headerList.add("学号");
        headerList.add("科目");

        System.out.println("原来的" + headerList);
        headerList1.add("打等第");
        headerList2.add("打星");
        headerList3.add("文字描述");

        headerList.addAll(headerList1);
        headerList.addAll(headerList2);
        headerList.addAll(headerList3);

        System.out.println("现在的" + headerList);
    }

    //string转list
    @Test
    public void stringToList() {
        String str = "61cc64f50574607648e23753,61cc64f50574607648e23752,60eab16863fae8b55d0hz008,61e7c8f9f70a66358feec7c7";
        System.out.println("String的值：" + str);
        List<String> list1 = Arrays.asList(str.split(","));
        System.out.println("subtract的值：" + list1.size());
        List<String> list2 = new ArrayList<String>();
        list2.add("2222");

        Collection<String> subtract = CollectionUtils.union(list1, list2);
        System.out.println("subtract的值：" + StringUtils.join(subtract, ","));
    }

}
